<?php

namespace App\Admin\Forms;

use App\Models\SwtBjCabinet;
use App\Models\SwtBjMachine;
use App\Services\BaoJianService;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;

class SettingMachineForm extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        $id = $this->payload['id'] ?? null;

        $machine = SwtBjMachine::firstWhere('id',$id);

        $res = BaoJianService::volumeSetting($input['device_code'], $input['media_volume']);

        if($res) {
            $machine -> media_volume = $input['media_volume'];
            $machine -> upload_mode = $input['upload_mode'];
            $machine -> save();
        }

        $res = BaoJianService::machineSetting($input);

        if($res) {
            foreach($input['cabinet_camera_list'] as $v) {
                $cabinet_update = [
                    'resolution'      => $v['resolution'],
                    'frame_rate'      => $v['frame_rate'],
                    'exposure'        => $v['exposure'],
                    'bit_stream'      => $v['bit_stream']
                ];

                SwtBjCabinet::where('id',$v['id']) -> update($cabinet_update);
            }
        }

        BaoJianService::machineRestart($input['device_code']);

        return $this->response()->success('设置成功')->refresh();
    }

    public function form()
    {
        $this -> hidden('device_code');

        $this -> radio('upload_mode','上传方式')
            -> options([1 => '上传主摄视频', 2 => '上传副摄视频', 100 => '全部上传'])
            -> rules('required', [ 'required' => '上传方式不能为空' ])
            -> required();

        $this -> slider('media_volume','媒体音量')
            -> options(['max' => 100, 'min' => 0, 'step' => 5]);

        $this -> table('cabinet_camera_list','货柜', function(NestedForm $table) {
            $table -> hidden('id');

            $table -> hidden('cabinet_no');

            $table -> hidden('camera_no');

            $table -> display('cabinet_no', '货柜号');

            $table -> display('camera_no', '摄像号');

            $table -> text('bit_stream','码流')
                -> type('number')
                -> rules('required', [ 'required' => '码流不能为空'])
                -> attribute('min', 2)
                -> attribute('max', 6)
                -> required();

            $table -> select('resolution','分辨率')->options([
                720  => '720',
                1080 => '1080'
            ]);

            $table -> select('frame_rate','帧率')->options([
                15 => '15帧',
                20 => '20帧',
                25 => '25帧',
            ]);

            $table -> text('exposure','曝光')
                -> type('number')
                -> rules('required', [ 'required' => '主摄曝光不能为空'])
                -> attribute('min', 0)
                -> attribute('max', 24)
                -> required();


        }) -> disableDelete() -> disableCreate() -> width(800) -> setFieldClass(['table-text-center']);
    }

    public function default()
    {
        $id = $this->payload['id'];

        $machine_info = SwtBjMachine::with('cabinet') -> firstWhere('id', $id);

        $cabinet_camera_list = $machine_info -> cabinet -> toArray();

        $data = [
            'device_code'         => $machine_info->device_code,
            'upload_mode'         => $machine_info->upload_mode,
            'media_volume'        => $machine_info->media_volume,
            'cabinet_camera_list' => $cabinet_camera_list
        ];

        return $data;
    }
}
